package com.forum.module.chat.controller;

import com.forum.framework.common.pojo.DataResult;
import com.forum.module.chat.controller.vo.ChatAskReqVO;
import com.forum.module.chat.controller.vo.ChatMessageRespVO;
import com.forum.module.chat.convert.chat.ChatMessageConvert;
import com.forum.module.chat.service.ChatService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.List;

import static com.forum.framework.common.pojo.DataResult.success;

/**
 * 功能:
 * 作者:ouyzh
 * 日期:2025/3/6 20:25
 */
@RestController
@RequestMapping("/chat")
public class ChatController {
    private final ChatService chatService;

    public ChatController(ChatService chatService) {
        this.chatService = chatService;
    }

    @PostMapping("/ask")
    public SseEmitter askQuestion(@RequestBody ChatAskReqVO reqVO) {
        SseEmitter emitter = new SseEmitter(180_000L); // 设置3分钟超时
        chatService.streamAnswer(reqVO.getQuestion(), emitter);
        return emitter;
    }

    @GetMapping("/history")
    public DataResult<List<ChatMessageRespVO>> history() {
        return success(ChatMessageConvert.INSTANCE.covertList(chatService.history()));
    }
}
